#!/usr/bin/perl

package DB::Oracle;

use strict;
use warnings;

use Carp qw(croak);
use DBI;
use Utils qw(dd);
use Log;

use parent 'DB::Base';

sub new
{
    my $class = shift;
    my $self = $class->SUPER::new(@_);
    return $self;
}

sub getCommand
{
    my $self = shift;
    return sprintf(
        "sqlplus %s/'%s'@%s:%s/%s",
        $self->{_config}->{user},
        $self->{_config}->{pass},
        $self->{_config}->{host},
        $self->{_config}->{port},
        $self->{_config}->{service}
    );
}

sub connectDB
{
    my $self = shift;

    my $dbh = DBI->connect(
        # "dbi:Oracle:host=$self->{_config}->{host};port=$self->{_config}->{port};service_name=$self->{_config}->{service}",
        "dbi:Oracle:$self->{_config}->{host}:$self->{_config}->{port}/$self->{_config}->{service}",
        $self->{_config}->{user},
        $self->{_config}->{pass},
        {
            RaiseError => 1,
            AutoCommit => 1,
            ora_charset => 'AL32UTF8',
            ora_nls_lang => 'AMERICAN_AMERICA.AL32UTF8',
        }
    ) or croak "Failed to connect to database: $DBI::errstr";
    $self->{dbh} = $dbh;
}

sub getVersion
{
    my $self = shift;
    my $dbh = $self->{dbh};
    my $res = $dbh->selectrow_array("SELECT VERSION FROM V\$INSTANCE");
    return $res;
}

1;
